/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>>ret;
        queue<TreeNode*>q;
        if(root)q.push(root);
        while(q.size())
        {
            vector<int>tmp;
            int sz=q.size();
            while(sz--)
            {
                auto node=q.front();
                q.pop();
                tmp.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            ret.push_back(tmp);
        }
        reverse(ret.begin(),ret.end());
        return ret;
    }
};
